﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace CommonLibrary
{
    public class ValidationCode
    {
        private int _imageHeight = 30;
        private int _fontSize = 18;
        private int _fontNum = 5;
        public int ImageHeight
        {
            get { return _imageHeight; }
            set { _imageHeight = value; }
        }

        public int ImageFontSize
        {
            get { return _fontSize; }
            set { _fontSize = value; }
        }

        public int ImagefontNumber
        {
            get { return _fontNum; }
            set { _fontNum = value; }
        }
        private string CreateValidationCode()
        {
            int number;
            char code;
            string strValiCode = String.Empty;

            System.Random random = new Random();

            for (int i = 0; i < _fontNum; i++)
            {
                number = random.Next();

                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));

                strValiCode += code.ToString();
            }
            return strValiCode;
        }



        public  string CreateValidationCodeImage()
        {
            string strValiCode = string.Empty;
            //创建图片大小
            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((_fontNum *( _fontSize+4.5))), _imageHeight);
            Graphics g = Graphics.FromImage(image);

            try
            {
                //生成随机生成器
                Random random = new Random();

                //清空图片背景色
                g.Clear(Color.White);

                //画图片的背景噪音线
                for (int i = 0; i <10; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    if (i % 2 == 0)
                    {
                        g.DrawLine(new Pen(Color.Gray), x1, y1, x2, y2);
                    }
                    else
                    {
                        g.DrawLine(new Pen(Color.YellowGreen), x1, y1, x2, y2);
                    }
                }

                Font font = new System.Drawing.Font("Arial",_fontSize, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                int intRandColor = random.Next(5);
                System.Drawing.Drawing2D.LinearGradientBrush brush = null;
                switch (intRandColor)
                {
                    case 1:
                        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.Green, 2.8f, true);
                        break;
                    case 2:
                        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Green, Color.Blue, 2.8f, true);
                        break;
                    case 3:
                        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.LightSkyBlue, Color.Red, 2.8f, true);
                        break;
                    case 4:
                        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.LightGreen, 2.8f, true);
                        break;
                    default: 
                        brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Green, Color.Red, 2.8f, true);
                        break;
                }
                //g.DrawString(strValiCode, font, brush, 2, 2);

                int intLeft = 0;
                int intLeft_padding = random.Next(5*_fontNum);
                for (int i = 0; i < _fontNum; i++)
                {
                    char chCode;
                    int intNumber= random.Next();

                    if (intNumber % 2 == 0)
                        chCode = (char)('0' + (char)(intNumber % 10));
                    else
                        chCode = (char)('A' + (char)(intNumber % 26));
                    intLeft = i * (_fontSize-3);

                    #region 过滤0

                    if (chCode == '0')
                    {
                        chCode = '3';
                    }
                    if (chCode == 'o' || chCode == 'O')
                    {
                        chCode = 'a';
                    }

                    #endregion


                    #region 过滤1

                    if (chCode == '1')
                    {
                        chCode = '4';
                    }

                    if (chCode == 'i' || chCode == 'I')
                    {
                        chCode = 'b';
                    }

                    #endregion


                    #region 过滤2

                    if (chCode == '2')
                    {
                        chCode = '5';
                    }

                    if (chCode == 'z' || chCode == 'Z')
                    {
                        chCode = 'c';
                    }

                    #endregion


                    g.DrawString(chCode.ToString(), font, brush, intLeft+intLeft_padding, random.Next(6));
                    strValiCode+=chCode.ToString();
                }

                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    if (i % 2 == 0)
                    {
                        image.SetPixel(x, y, Color.GreenYellow);
                    }
                    else
                    {
                        image.SetPixel(x, y, Color.Red);
                    }
                }
                //清除该页输出缓存，设置该页无缓存
                System.Web.HttpContext.Current.Response.Buffer = true;

                System.Web.HttpContext.Current.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);

                System.Web.HttpContext.Current.Response.Expires = 0;

                System.Web.HttpContext.Current.Response.CacheControl = "no-cache";

                System.Web.HttpContext.Current.Response.AppendHeader("Pragma", "No-Cache");


                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                System.Web.HttpContext.Current.Response.ClearContent();
                System.Web.HttpContext.Current.Response.ContentType = "image/Gif";
                System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
            return strValiCode;
        }


    }
}
